<template>
  <el-dialog title="添加页面" :visible="showDialog" @close="closeFn">
    <el-form ref="form" :model="formData" :rules="rules">
      <el-form-item label="标题" label-width="80px" prop="title">
        <el-input v-model="formData.title" />
      </el-form-item>
      <el-form-item label="内容" label-width="80px" prop="content">
        <!-- 富文本 -->
        <quillEditor v-model="formData.content" @blur="$refs.form.validateField('content')" />
      </el-form-item>
      <el-form-item>
        <el-row type="flex" justify="end">
          <el-button type="primary" size="small" @click="confirmFn">确定</el-button>
          <el-button size="small" @click="closeFn">取消</el-button>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>
<script>
import { addPAge, getPageDetails, updatePage } from '@/api/pagemanage'
import { quillEditor } from 'vue-quill-editor' // 调用编辑器
import 'quill/dist/quill.core.css'
import 'quill/dist/quill.snow.css'
import 'quill/dist/quill.bubble.css'
export default {
  components: {
    quillEditor
  },
  props: {
    showDialog: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      // 校验规则
      rules: {
        title: [{ required: true, message: '请输入标题', trigger: 'blur' }],
        content: [{ required: true, message: '请输入内容', trigger: 'blur' }]
      },
      formData: {
        title: '',
        content: ''
      }
    }
  },
  computed: {
  },
  watch: {
  },
  methods: {
    // 关闭弹窗
    closeFn() {
      this.$emit('update:showDialog', false)
      // 清空表单
      this.formData = {
        title: '',
        content: ''
      }
      // 重置表单
      this.$refs.form.resetFields()
      //  刷新列表
      this.$emit('uploadList')
    },
    async confirmFn() {
      // 校验
      await this.$refs.form.validate()
      // 调用接口
      if (this.formData.id) {
        // 编辑
        await updatePage(this.formData)
      } else {
        // 新增
        await addPAge(this.formData)
      }
      // 关闭弹窗
      this.closeFn()
    },
    // s
    async getPageDetail(id) {
      const res = await getPageDetails(id)
      this.formData = res.data
    }
  }
}
</script>
<style lang="scss" scoped>
::v-deep .quill-editor {
    // height:150px;
}
</style>
